package com.example.springboot2.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot2.base.web.BaseController;
import com.example.springboot2.oa.entity.TDiary;
import com.example.springboot2.oa.entity.TUser;
import com.example.springboot2.oa.service.impl.TDiaryService;
import com.example.springboot2.tools.MakeResponse;
import com.example.springboot2.tools.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;

@CrossOrigin
@RestController
@RequestMapping("/oa/t-diary")
public class TDiaryController extends BaseController {
    @Autowired
    private TDiaryService diaryService;

    /**
     * 用户列表
     * @param request
     * @return
     */
    @PostMapping("listAll")
    public ResponseMessage listAll(HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        QueryWrapper<TDiary> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("create_time");
        List<TDiary> data = diaryService.list(wrapper);
        return MakeResponse.success("200", "成功", data);
    }

    /**
     * 添加用户
     */
    @PostMapping("add")
    public ResponseMessage add(@RequestBody TDiary diary, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        QueryWrapper<TDiary> wrapper = new QueryWrapper<>();

        diary.setDiaryId(String.valueOf(UUID.randomUUID()));
        diary.setCreateTime(new Date());
        diary.setDiaryState(0);

        if (!diaryService.save(diary)) {
            return MakeResponse.success("500", "新增失败");
        }

        return MakeResponse.success("200", "新增成功");
    }

    /**
     * 批量删除用户
     * @param diaryList
     * @param request
     * @return
     */
    @PostMapping("delete")
    public ResponseMessage delete(@RequestBody List<TDiary> diaryList, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        List<String> diaryIdList = new ArrayList<>();
        diaryList.forEach(new Consumer<TDiary>() {
            @Override
            public void accept(TDiary diary) {
                diaryIdList.add(diary.getDiaryId());
            }
        });

        QueryWrapper<TDiary> wrapper = new QueryWrapper<>();
        wrapper.in("diary_id", diaryIdList);
        if (!diaryService.remove(wrapper)) {
            return MakeResponse.success("500", "批量删除失败");
        }

        return MakeResponse.success("200", "批量删除成功");
    }

    /**
     * 更新用户
     * @param diary
     * @param request
     * @return
     */
    @PostMapping("modify")
    public ResponseMessage modify(@RequestBody TDiary diary, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        diary.setUpdateTime(new Date());
        QueryWrapper<TDiary> wrapper = new QueryWrapper<>();
        wrapper.eq("diary_id", diary.getDiaryId());
        if (!diaryService.update(diary, wrapper)) {
            return MakeResponse.success("500", "修改失败");
        }

        return MakeResponse.success("200", "修改成功");
    }

    /**
     * 更新状态
     * @param diary
     * @param request
     * @return
     */
    @PostMapping("state")
    public ResponseMessage state(@RequestBody TDiary diary, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        diary.setDiaryState(1);
        QueryWrapper<TDiary> wrapper2 = new QueryWrapper();
        wrapper2.eq("diary_id", diary.getDiaryId());

        if (!diaryService.update(diary, wrapper2)) {
            return MakeResponse.success("500", "设置状态失败");
        }

        return MakeResponse.success("200", "设置状态成功");
    }
}
